package com.tf.temper.controller;

import com.ccssoft.m8.common.ccssoft.modules.util.EntityWrapperHandle;
import com.ccssoft.m8.common.web.BaseController;
import com.tf.temper.entity.*;
import com.tf.temper.service.DeviceSettingService;
import com.tf.temper.service.LogeventService;
import com.tf.temper.service.UserinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 上传比对结果
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "上传人脸比对结果",description="DevuploadresultController")
@RestController
@RequestMapping("/syface")
public class DevuploadresultController extends BaseController
{
    @Autowired
    private LogeventService<LogeventEntity> logeventService;
    @Autowired
    private DeviceSettingService<DeviceSettingEntity> deviceSettingService;
    @Autowired
    private UserinfoService<UserinfoEntity> userinfoService;
    @ApiOperation("上传人脸比对结果接口")
    @PostMapping("/{deviceid}")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public SyfaceReturnEntity<String> syface(@PathVariable("deviceid") String deviceid, @RequestBody UploadsyfaceEntity faceentity)
    {
        if(deviceid!=null)
        {
            EntityWrapperHandle<DeviceSettingEntity> ewdevicentity = new EntityWrapperHandle<DeviceSettingEntity>();
            ewdevicentity.eq("deviceid", deviceid);
            DeviceSettingEntity curdevicesetting =  deviceSettingService.selectOne(ewdevicentity);
            if(curdevicesetting!=null&&faceentity!=null)
            {
                int unlockDelay = curdevicesetting.getUnlockDelay();
                int mask = curdevicesetting.getMask();
                String temperature = curdevicesetting.getTemperature();
                int openDoor = 0;
                if((mask==1&&faceentity.isMask()==true)||mask==0)
                {
                   double settemperature =  Double.parseDouble(temperature);
                   double curtemperature  =  Double.parseDouble(faceentity.getTemperature());
                   if(curtemperature<settemperature)
                   {
                       openDoor = 1;
                   }
                }
                LogeventEntity templogevent = new LogeventEntity();
                templogevent.setUserId(faceentity.getUserId());

                EntityWrapperHandle<UserinfoEntity> ewuseinfo = new EntityWrapperHandle<UserinfoEntity>();
                ewuseinfo.eq("userid", faceentity.getUserId());
                List<UserinfoEntity> tmpuserinfo =  userinfoService.selectList(ewuseinfo);
                if(tmpuserinfo!=null&&tmpuserinfo.size()>0)
                {
                    templogevent.setUserName(tmpuserinfo.get(0).getUsername());
                    templogevent.setUserType(tmpuserinfo.get(0).getUsertype());
                    templogevent.setSex(tmpuserinfo.get(0).getSex());
                    templogevent.setTemplateimgurl(tmpuserinfo.get(0).getUserimageurl());
                }
                else
                {
                    templogevent.setUserName(faceentity.getUserName());
                    templogevent.setUserType(faceentity.getUserType());
                }
                templogevent.setAction(faceentity.getAction());
                templogevent.setActionCode(faceentity.getActionCode());
                templogevent.setTemperature(faceentity.getTemperature());
                templogevent.setDeviceid(deviceid);
                templogevent.setMask(faceentity.isMask()==false?1:0);
                if(faceentity.getOpenCode()==100)
                {
                    templogevent.setOpenCode(1);
                }
                else
                {
                    templogevent.setOpenCode(0);
                }
                templogevent.setOpenMsg(faceentity.getOpenMsg());
                templogevent.setUuid(faceentity.getUuid());
                templogevent.setNationCode(faceentity.getNationCode());
                templogevent.setScreenTime(faceentity.getScreenTime());
                templogevent.setSimilarity(faceentity.getSimilarity());
                try
                {
                    BASE64Decoder decoder = new BASE64Decoder();
                    String format = "yyyy-MM-dd";
                    SimpleDateFormat sdf = new  SimpleDateFormat(format);
                    String curDate =  sdf.format(new Date());
                    String imgpath = curDate+deviceid+System.currentTimeMillis()+".jpg";
                    File file = new File("/images/"+imgpath);
                    if (!file.exists())
                    {
                        file.createNewFile();
                    }
                    byte[] buf = decoder.decodeBuffer(faceentity.getImg());
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    fileOutputStream.write(buf);
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    templogevent.setImgurl("http://healthy-frontend-gcgs-cx-gc-20200902.ccsyun.vip/dataimages/"+imgpath);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                logeventService.insert(templogevent);
                return new SyfaceReturnEntity<String>().ok("0",openDoor,unlockDelay);
            }
        }
        return new SyfaceReturnEntity<String>().error(" DeviceSetting is null",20);
    }
}